package com.eclink.iot.utils;

public class ByteUtils
{
  public static String toHexString(byte[] array, int index, int length, String separator) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
      sb.append(String.format("%02X", new Object[] { Byte.valueOf(array[index + i]) }));
      sb.append(separator);
    } 
    return sb.toString();
  }
  
  public static String toHexString(int[] array, int index, int length, String separator) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < length; i++) {
      sb.append(String.format("%02X", new Object[] { Integer.valueOf(array[index + i]) }));
      sb.append(separator);
    } 
    return sb.toString();
  }
  
  public static String toAsciiString(byte[] array, int index, int length) {
    try {
      byte[] buffer = new byte[length];
      System.arraycopy(array, index, buffer, 0, length);
      return (new String(buffer, "ASCII")).trim();
    } catch (Exception ex) {
      return "";
    } 
  }
  
  public static byte[] subString(byte[] array, int index, int length) {
    try {
      byte[] buffer = new byte[length];
      System.arraycopy(array, index, buffer, 0, length);
      return buffer;
    } catch (Exception ex) {
      return null;
    } 
  }
  
  public static byte[] toBytes(String value) {
    if (value.isEmpty()) {
      return null;
    }
    value = value.replace(" ", "");
    
    byte[] array = new byte[value.length() / 2];
    
    for (int i = 0; i < array.length; i++) {
      array[i] = (byte)Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16);
    }
    
    return array;
  }

  public static void printBytes(byte[] array) {
    for (int i = 0; i < array.length; i++) {
      System.out.print(array[i] + " ");
    }
    System.out.println();
  }
}


/* Location:              E:\code\work\eclink\eclink-iot-tcp-server\backend\libs\eclink-iot.jar!\com\eclin\\utils\ByteUtils.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */